#include <cmath>
#include <iostream>
#include <list>
#include <unordered_map>
#include <vector>
using namespace std;

class Solution
{
public:
    int totalFruit(vector<int>& fruits)
    {
        int left = 0;
        int right = 0;
        unordered_map<int,int> hash;
        int num = 0;

        while(right < fruits.size())
        {
            hash[fruits[right]]++;
            while(hash.size() > 2)
            {
                hash[fruits[left]]--;
                if(hash[fruits[left]] == 0)
                {
                    hash.erase(fruits[left]);
                }
                left++;
            }
            num = max(num, right-left+1);
            right++;
        }
        return num;
    }
};
